package org.erp.product.controller;

import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.erp.common.domain.Result;
import org.erp.common.page.DataPageResp;
import org.erp.common.page.PageQueryModel;
import org.erp.product.domain.bo.CallbackBo;
import org.erp.product.domain.bo.ConnectActionBo;
import org.erp.product.domain.bo.ConnectLogQueryBo;
import org.erp.product.domain.bo.TransferBo;
import org.erp.product.domain.vo.ConnectLogVo;
import org.erp.product.infrastructure.convert.ConnectConvert;
import org.erp.product.router.ConnectActionFacade;
import org.erp.product.router.DeviceEventRouter;
import org.erp.product.router.TransferSourceFacade;
import org.erp.product.service.ConnectLogService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static org.erp.common.domain.Result.success;

/**
 * 接驳记录控制层
 *
 * @author libin
 * @version 1.0
 * @description: 接驳记录控制层
 * @date 2025/11/13 16:33
 **/
@Log4j2
@RestController
@AllArgsConstructor
@RequestMapping("/connect")
public class ConnectLogController {

    private final ConnectLogService connectLogService;

    private final TransferSourceFacade transferSourceFacade;

    private final ConnectActionFacade connectActionFacade;

    private final DeviceEventRouter deviceEventRouter;

    private final ConnectConvert connectConvert;

    /**
     * 接驳记录列表
     *
     * @param connectLogQueryBo
     * @return
     */
    @PostMapping("/list")
    public Result<DataPageResp<ConnectLogVo>> pageList(@Validated @RequestBody ConnectLogQueryBo connectLogQueryBo) {
        PageQueryModel<ConnectLogVo> pageList = connectLogService.pageList(connectConvert.boConvertDto(connectLogQueryBo));
        return success(DataPageResp.of(pageList.getTotal(), pageList.getDataList()));
    }

    /**
     * 调拨
     *
     * @param transferBo
     * @return
     */
    @PostMapping("/transfer")
    public Result<String> transfer(@RequestBody TransferBo transferBo) {
        transferSourceFacade.transfer(connectConvert.boConvertDto(transferBo));
        return success();
    }

    /**
     * 回调接口
     *
     * @param callbackBo
     * @return
     */
    @PostMapping("/callback")
    public Result<String> callback(@RequestBody CallbackBo callbackBo) {
        deviceEventRouter.dispatch(connectConvert.boConvertDto(callbackBo));
        return success();
    }

    /**
     * 接驳事件
     * @param connectActionBo
     * @return
     */
    @PutMapping("/action")
    public Result<String> action(@RequestBody ConnectActionBo connectActionBo) {
        connectActionFacade.action(connectConvert.boConvertDto(connectActionBo));
        return success();
    }

}
